<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理 -->
      <el-row class="btn_container">
        <el-button size="mini" type="primary" @click="Addarole">添加角色</el-button>
      </el-row>
      <!-- 表格组件 -->
      <el-table v-loading="loading" :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">  {{ row.name }} </span>
            <el-input v-else v-model="row.editROW.name" size="mini" />
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <!-- 自定义列结构 -->
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
            <el-switch v-else v-model="row.editROW.state" size="mini" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">  {{ row.description }} </span>
            <el-input v-else v-model="row.editROW.description" placeholder="请输入描述" type="textarea" size="mini" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <!-- 放置操作按钮 -->
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="btnEDutrow(row)">编辑</el-button>
              <el-popconfirm title="确定要删除吗？" @onConfirm="delRole(row.id)">
                <template #reference>
                  <el-button size="mini" type="text" style="margin-left: 10px;">删除</el-button>
                </template>
              </el-popconfirm>
            </span>
            <span v-else>
              <el-button size="mini" type="primary" @click="btnOk(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row style="height:80px" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager, next , jumper , total"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <Addarole :show-dialog.sync="showDialog" @updateDept="getRoleList" />
  </div>
</template>
<script>
import { getRoleListAPI, delRoleAPI, updateRoleAPI } from '@/api'
import Addarole from './components/Addarole'
export default {
  name: 'Role',
  components: {
    Addarole
  },
  data() {
    return {
      showDialog: false,
      list: [],
      pageParams: {
        total: 0,
        pagesize: 5,
        page: 1
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleListAPI(this.pageParams)
      this.list = rows
      this.pageParams.total = total
      this.loading = false
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editROW', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    changePage(newPage) {
      this.pageParams.page = newPage
      this.getRoleList()
    },
    Addarole() {
      this.showDialog = true
    },
    async delRole(id) {
      await delRoleAPI(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) { this.pageParams.page-- }
      this.getRoleList()
    },
    btnEDutrow(row) {
      row.isEdit = !row.isEdit
      row.editROW.name = row.name
      row.editROW.state = row.state
      row.editROW.description = row.description
    },
    async btnOk(row) {
      if (row.editROW.name && row.editROW.description) {
        await updateRoleAPI(
          { id: row.id, ...row.editROW }
        )
        this.$message.success('角色更新成功')
        Object.assign(row, { ...row.editROW, isEdit: false })
      } else {
        this.$message.warning('角色名称和描述不能为空')
      }
    }
  }
}
</script>
<style scoped>
.btn_container {
  padding: 10px 20px;
}
</style>
